<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>成单客户列表</title>
    <c:import url="../../header.jsp"/>
    <style media="screen">
        /** 展开按钮 **/
        td.details-control {
            width: 20px;
            background: url('${basePath}/static/images/details_open.png') no-repeat center center;
            cursor: pointer;
        }
        td.details-control i {
            display: block;
            position: relative;
        }
        td.details-control .iconfont {
            font-size: 14px;
            font-weight: bold;
            color: #ccc;
        }

        /** 收起按钮 **/
        tr.shown td.details-control {
            background: url('${basePath}/static/images/details_close.png') no-repeat center center;
        }
        tr.shown+tr .dggui-table thead tr th {
            background: #fff;
            border-bottom: 1px #ebebeb solid;
        }
        .list-content .dggui-table tbody tr.shown+tr>td {
            padding: 0;
        }
        tr.shown+tr td .dggui-table tbody td {
            padding: 12px 10px;
        }
        .sub-table-warp {
            max-height: 240px;
            overflow: auto;
            background: #fff;
            padding-left: 40px;
        }
    </style>
</head>

<body>
<div class="content-warp">
    <div class="dgg-list-top-warp">
        <div class="screen-search">
        <ul>
            <li>
                <label>关键字：</label>
                <input type="text" class="dggui-input dggui-input-large" id="searchKey" name="searchKey"
                       placeholder="客户名/订单编号/生产订单编号" maxlength="50" style="width: 190px;">
            </li>
            <li>
                <label>签单商务：</label>
                <select id="signUserId" name="signUserId">
                </select>
            </li>
            <li>
                <label>签单部门：</label>
                <div class="tree-select-warp">
                    <input type="text" class="dggui-input input-selct-tree" id="signOrg" name="signOrg" data-id=""
                           value="" style="width: 320px"/>
                </div>
            </li>
            <li>
                <label>业务类型：</label>
                <input type="hidden" name="typeCodeHidden" id="typeCodeHidden">
                <div name="typeCode" id="typeCode"></div>
            </li>
            <li>
                <label>产品：</label>
                <select name="productCode" id="productCode" style="min-width: 80px; display: none;">
                </select>
            </li>
            <li>
                <label>渠道：</label>
                <select name="channelCode" id="channelCode" class="dggui-select">
                    <option value=''>请选择</option>
                </select>
            </li>
            <li>
                <label>放款金额：</label>
                <input type="number" class="dggui-input dggui-input-large" id="moneyMin" name="moneyMin"
                       placeholder="输入最小金额" min="0">~
                <input type="number" class="dggui-input dggui-input-large" id="moneyMax" name="moneyMax"
                       placeholder="输入最大金额" min="0">
            </li>
            <li>
                <label>放款时间：</label>
                <input type="text" readonly class="dggui-input dggui-time-range" id="loanTime" name="loanTime">
                <i class="iconfont icon-rili"></i>
            </li>
            <li>
                <label>还款到期时间：</label>
                <input type="text" readonly class="dggui-input dggui-time-range" id="repaymentDate" name="repaymentDate">
                <i class="iconfont icon-rili"></i>
            </li>
            <li>
                <button class="dggui-btn searchBtn" onclick="tableReload()">搜索</button>
                <button class="dggui-btn dggui-btn-primary" onclick="cleanSearch()">清空条件</button>
                <d:hasUrlAuth authCode="RZSC_OPERATION_EXPORT">
                    <button class="dggui-btn dggui-btn-green"
                            onclick="excelExportCompleteCustomer('${basePath}/rzsc/complete_customer/excel_complete_customer.do')">导出
                    </button>
                </d:hasUrlAuth>
            </li>
        </ul>
<%--
        <span class="text-red" style="padding: 4px 8px;background-color: #eeeeee;color: red">
            <span class="iconfont"></span>提示：由于数据量过大，请选择分公司进行查询。</span>--%>
        </div>
    </div>
    <div class="list-content">
        <div class="table-warp">
            <table class="dggui-table dataTable no-footer" id="myTable" class="display">

            </table>
        </div>
    </div>

</div>
<c:import url="../../footer.jsp"/>

<script type="text/javascript">
    var tableObj;
    dggui.use(['table', 'distpicker', 'dggdate','select2','droptree'], function() {
        var table = dggui.table;
        window.$ = dggui.jquery;
        var droptree = dggui.droptree;
        var dggdate = dggui.dggdate;
        var select2 = dggui.select2;
        var distpicker = dggui.distpicker;
        //初始化签单商务
        initUser("bus_advisor", "role", "#signUserId");

        select2.render({
            placeholder: "请选择",
            allowClear: true,
            elem: $('#signUserId'),
            width: "160px"
        });
        //初始化签单时间
        dggdate.render({
            elem: '#signTime',
            type: 'date',
            range: '~'
        });
        // 加载部门下拉框
        queryAllDep("#signOrg");
        //初始化业务类型及产品
        distpicker.render({
            url: "${basePath}/rzsc/common/tree_book_list.do?code=BUS_YT_DK&type=2&level=2", // 请求地址
            elem: '#typeCode', // 容器Id
            type: 2,
            placeholder: '选择业态',
            defaultParentCode: 'BUS_YT_DK',
            callback: function () {//如果不需要选产品就把callback整个方法注释掉
                var code = $("#typeCode .plug-in-code").val();
                initProductCode("#productCode", code);
                select2.render({
                    placeholder: "请选择",
                    allowClear: true,
                    elem: $('#productCode'),
                    width: "160px"
                });
                var produtId = $("#productCode").val();
                initChannel("#channelCode", produtId);
                select2.render({
                    placeholder: "请选择",
                    allowClear: true,
                    elem: $('#channelCode'),
                    width: "160px"
                });
            },
            callbackClear: function () {
                $("#productCode").find("option").remove();
                $("#productCode").append("<option value=''>请选择</option>");
                $("#channelCode").find("option").remove();
                $("#channelCode").append("<option value=''>请选择</option>");
            }
        });
        select2.render({
            placeholder: "请选择",
            allowClear: true,
            elem: $('#productCode'),
            width: "160px"
        });
        select2.render({
            placeholder: "请选择",
            allowClear: true,
            elem: $('#channelCode'),
            width: "160px"
        });
        //渠道初始化
        $('#productCode').on('change', function (e) {
            var produtId = $("#productCode").val();
            initChannel("#channelCode", produtId);
            select2.render({
                placeholder: "请选择",
                allowClear: true,
                elem: $('#channelCode'),
                width: "160px"
            });
        });
        //初始化放款时间
        dggdate.render({
            elem: '#loanTime',
            type: 'date',
            range: '~'
        });
        //初始化还款到期日
        dggdate.render({
            elem: '#repaymentDate',
            type: 'date',
            range: '~'
        });
        tableObj = $('#myTable').dataTableExtend({
            "elem":'#myTable',
            "ajax": sysInfo.basePath + "/rzsc/complete_customer/complete_customer_list.do",
            "scrollX": "true",//是否开启横项滚动
            "oneSelectCheck": true, //单选，同时选中复选框
            "ordering": false,
            "fnServerParams": function (aoData) {
                var moneyMax = $("#moneyMax").val().trim();
                var moneyMin = $("#moneyMin").val().trim();
                //搜索条件
                aoData = $.extend(aoData, {
                    searchKey: $("#searchKey").val(),
                    signUserId: $("#signUserId").val(),
                    signOrgId: $("#signOrg").attr("data-id"),
                    typeCode: $("#typeCode .plug-in-code").val(),
                    productId: $("#productCode").val(),
                    channelId: $("#channelCode").val(),
                    loanTime:$("#loanTime").val(),
                    repaymentDate:$("#repaymentDate").val(),
                    moneyMax: moneyMax,
                    moneyMin: moneyMin
                })
                return aoData;
            },
            "aoColumns": [
                {
                    "sClass":"details-control",
                    "width":"20px",
                    "render":function(data,type,row){
                        return '<i></i>'
                    }
                },
                {
                    'title': '当前商机',
                    "data": "businessNo",
                    "render": function (data, type, row, meta) {
                        return data;
                    }
                },
                {
                    'title': '客户名称',
                    "data": "customerName",
                    "render": function (data, type, row, meta) {
                        if(isEmpty(data)||data=='null'){
                            data = "异常客户";
                        }
                        return data;
                    }
                },
                {
                    'title': '客户编号',
                    "data": "customerNo",
                    "render": function (data, type, row, meta) {
                        return data;
                    }
                },
                {
                    'title': '联系号码',
                    "data": "customerPhone",
                    "render": function (data, type, row, meta) {
                        return data;
                    }
                },
                {
                    'title': '身份证号',
                    "data": "customerCardId",
                    "render": function (data, type, row, meta) {
                        return data;
                    }
                },
                {
                    'title': '商机所属商务',
                    "data": "belongUser",
                    "render": function (data, type, row, meta) {
                        return data;
                    }
                },
                {
                    'title': '部门',
                    "data": "belongUserOrg",
                    "render": function (data, type, row, meta) {
                        return data;
                    }
                },
                {
                    'title': '操作',
                    "render": function (data, type, row, meta) {
                        var htm = '<a href="javascript:void(0)" class="dggui-link" onclick="lookDetail(\'' + row.customerId + '\',\'' + row.customerName + '\');">查看详情</a>';
                        return htm;
                    }
                }
            ],
        })

        //给每一行添加展开或收起的监听
        $('#myTable tbody').on('click', 'td.details-control', function () {
            var tr = $(this).closest('tr');
            var row = tableObj.row( tr );

            if ( row.child.isShown() ) {
                //如果该行已经打开，则关闭
                row.child.hide();
                tr.removeClass('shown');
            } else {
                //打开该行
                /*根据条目数据返回需要显示的订单列表*/
                var customerId = row.data().customerId;
                var searchKey = $("#searchKey").val();
                var signUserId = $("#signUserId").val();
                var signOrgId = $("#signOrg").attr("data-id");
                var typeCode = $("#typeCode .plug-in-code").val();
                var productId = $("#productCode").val();
                var channelId= $("#channelCode").val();
                var loanTime=$("#loanTime").val();
                var repaymentDate=$("#repaymentDate").val();
                var moneyMax = $("#moneyMax").val().trim();
                var moneyMin = $("#moneyMin").val().trim();
                $.ajax({
                    url: sysInfo.basePath + "/rzsc/complete_customer/find_scorder_by_customer_id.do",
                    type: 'POST',
                    async: true,
                    dataType: "json",
                    data: {customerId:customerId,searchKey:searchKey,signUserId:signUserId,signOrgId:signOrgId,
                        typeCode:typeCode,productId:productId,channelId:channelId,loanTime:loanTime,repaymentDate:repaymentDate,
                        moneyMax:moneyMax,moneyMin:moneyMin },
                    success: function (result) {
                        if (result.code == 0) {
                            var productOrders = result.data;
                            var ralaGoodsHtm = '<div class="sub-table-warp" style="margin-top: -20px;">' +
                                '<table class="dggui-table" style="border: 1px solid #e0e0e0;">' +
                                '<thead><tr>' +
                                '<th>销售订单编号</th>' +
                                '<th>生产订单编号</th>' +
                                '<th>业务类型</th>' +
                                '<th>产品</th>' +
                                '<th>渠道</th>' +
                                '<th>生产事业部</th>' +
                                '<th>流程人员</th>' +
                                '<th>签单商务</th>' +
                                '<th>放款金额</th>' +
                                '<th>放款时间</th>' +
                                '<th>每月还款日期</th>' +
                                '<th>还款到期时间</th>' +
                                '</tr></thead><tbody>';
                            for(var i = 0; i < productOrders.length; i++){
                                ralaGoodsHtm += '<tr>' +
                                    '<td>'+productOrders[i].orderNo+'</td>' +
                                    '<td>'+productOrders[i].scProductOrderNo+'</td>' +
                                    '<td>'+productOrders[i].businessProductName+'</td>' +
                                    '<td>'+productOrders[i].productName+'</td>' +
                                    '<td>'+productOrders[i].channelName+'</td>' +
                                    '<td>'+productOrders[i].productOrgName+'</td>' +
                                    '<td>'+productOrders[i].flowUserName+'</td>' +
                                    '<td>'+productOrders[i].signUserName+'</td>' +
                                    '<td>'+productOrders[i].loanMoneyStr+'</td>' +
                                    '<td>'+productOrders[i].loanTimeStr+'</td>' +
                                    '<td>'+productOrders[i].dateOfLoanStr+'</td>' +
                                    '<td>'+productOrders[i].repaymentDateStr+'</td>' +
                                    '</tr>';
                            }

                            ralaGoodsHtm += '</tbody></table></div>';
                            row.child(ralaGoodsHtm).show();
                            tr.addClass('shown');
                            tr.next().addClass('selected');
                        }else {
                            dggui.dialog.errors(result.msg);
                        }
                    }
                })
            }
        });
    })

    //列表刷新
    function tableReload() {
        var moneyMax = $("#moneyMax").val().trim();
        var moneyMin = $("#moneyMin").val().trim();
        if (!checkMoneySearch(moneyMin, moneyMax)) {
            return false;
        }
        tableObj.ajax.reload();
    }

    /**
     * 跳转到客户详情
     * @param customerId 客户id
     */
    function lookDetail(customerId,customerName) {
        customerName = customerName.replace("<","&lt;");
        customerName = customerName.replace(">","&gt;");
        window.parent.updateTab('客户详情:' + customerName, sysInfo.basePath +
            "/rzsc/complete_customer/complete_customer_detail.html?customerId=" + customerId);
    }

    /**
     * 成单客户导出
     */
    function excelExportCompleteCustomer(url) {
        var obj = new Object();
        var moneyMax = $("#moneyMax").val().trim();
        var moneyMin = $("#moneyMin").val().trim();
        obj.searchKey = $("#searchKey").val();
        obj.signUserId = $("#signUserId").val();
        obj.signOrgId = $("#signOrg").attr("data-id");
        obj.typeCode = $("#typeCode .plug-in-code").val();
        obj.productId = $("#productCode").val();
        obj.channelId = $("#channelCode").val();
        obj.loanTime = $("#loanTime").val();
        obj.repaymentDate = $("#repaymentDate").val();
        obj.moneyMax = moneyMax;
        obj.moneyMin = moneyMin;
        if (!checkMoneySearch(moneyMin, moneyMax)) {
            return false;
        }
        formSubmit(url, obj);
    }
</script>
</body>
</html>